package development;

import state.GameAlert;
import state.GameConstants;
import state.GameForm;
import state.GameGlobal;
import state.GameImage;
import state.Graphics;
import state.Key;
import state.TimerListener;
import state.component.Event;
import state.component.EventListener;
import state.component.MultiTab;
import state.component.TextField;
import state.font.FontManager;
import state.font.ImageFont;
import state.util.GameColor;
import state.util.Hashtable;
import state.util.ImageUtil;
import state.util.NumberUtil;
import state.util.array.StringArray;
import development.group.FrmGroupMenu;
import development.group.FrmGroupNapXuAndExchangeGold;
import development.socket.SocketClientUtil;

public class FrmMoneyToGoldExchange extends GameForm implements TimerListener, EventListener {
  private static GameForm singleton;
  
  private float exchangeRate = 2000;
  private boolean isReceivedExchangeRate;
  
  private TextField moneyTextField;
  private ImageFont text8;
  private GameImage popupImage;
  
  private int gold;
  
  public static GameForm instance() {
    if (singleton == null) {
      singleton = new FrmMoneyToGoldExchange();
    }
    return singleton;
  }

  public void init(Hashtable parameters) {
    isReceivedExchangeRate = false;
    SocketClientUtil.exchangeRateRequest();
    SocketClientUtil.requestUserInfo(Global.currentUser.getName());
    
    popupImage = ImageUtil.getImage(Constants.NEW_IMAGE_CATEGORY, "bg_popup.png");
    
    moneyTextField = new TextField(_manager, TextField.CONSTRAINT_NUMERIC, Constants.TEXT_FIELD_STYLE);
    moneyTextField.setPosition(15, 50);
    moneyTextField.setSize(45, 20);
    moneyTextField.setMaxLength(5);
    moneyTextField.setVisible(false);
    moneyTextField.setFocused(true);
    
    text8 = FontManager.getFont(FontManager.FONT_SIZE_8);
    setTimerListener(this);
  }
  
  public void onExchangeRateResponse(float exchangeRate1) {
    this.exchangeRate = exchangeRate1;
    isReceivedExchangeRate = true;
    moneyTextField.setVisible(true);
    moneyTextField.setFocused(true);
  }
  
  public void onChangeMoneyToGoldSuccess() {
    Global.alert.showAlert(this, GameAlert.OK_TYPE, "Đổi tiền ra gold thành công");
    moneyTextField.setText("");
  }
  
  public void onChangeMoneyToGoldFail() {
    Global.alert.showAlert(this, GameAlert.OK_TYPE, "Đổi tiền ra gold thất bại");
  }
  
  public void draw(Graphics g) {
    DrawUtils.drawBackground(g);
    if (!isReceivedExchangeRate) {
      g.drawImage(popupImage, GameConstants.SCREEN_WIDTH / 2, GameConstants.SCREEN_HEIGHT / 2, GameConstants.CENTER_ANCHOR);
      text8.drawString2(g, "Đang cập nhật", GameColor.WHITE_CODE, GameConstants.SCREEN_WIDTH / 2, GameConstants.SCREEN_HEIGHT / 2, GameConstants.CENTER_ANCHOR);
      text8.drawString2(g, "tỷ lệ quy đổi", GameColor.WHITE_CODE, GameConstants.SCREEN_WIDTH / 2, GameConstants.SCREEN_HEIGHT / 2 + 20, GameConstants.CENTER_ANCHOR);
      return;
    }
    
    int textY = moneyTextField.getRealY() + moneyTextField.getHeight() / 2;
    int endX = text8.drawString2(g, "đổi thành", GameColor.WHITE_CODE, moneyTextField.getRealX() + moneyTextField.getWidth() + 15, textY, Constants.VCENTER_LEFT_ANCHOR);
    text8.drawString2(g, gold + " gold", GameColor.YELLOW_CODE, endX + 10, textY, Constants.VCENTER_LEFT_ANCHOR);
    
    text8.drawString2(g, "(Tỷ lệ quy đổi 1$ : " + (int) exchangeRate + " gold)", GameColor.WHITE_CODE, 15, textY + 30, Constants.VCENTER_LEFT_ANCHOR);
    
    text8.drawString2(g, "Hiện đang có:", GameColor.WHITE_CODE, 15, textY + 50, Constants.VCENTER_LEFT_ANCHOR);
    text8.drawString2(g, "- ", GameColor.WHITE_CODE, 50, textY + 70, Constants.VCENTER_LEFT_ANCHOR);
    text8.drawString2(g, String.valueOf(Global.currentUser.getMoney()) + "$", GameColor.YELLOW_CODE, 60, textY + 70, Constants.VCENTER_LEFT_ANCHOR);
    text8.drawString2(g, "- ", GameColor.WHITE_CODE, 50, textY + 90, Constants.VCENTER_LEFT_ANCHOR);
    text8.drawString2(g, String.valueOf(Global.currentUser.getGold()) + " gold", GameColor.YELLOW_CODE, 60, textY + 90, Constants.VCENTER_LEFT_ANCHOR);
  }

  public void keyReleased(int keyCode) {
    switch (keyCode) {
    case Key.FIRE:
      moneyTextField.pointerReleased(moneyTextField, moneyTextField.getRealX(), moneyTextField.getRealY());
      break;
    case Key.SOFT_RIGHT:
      if (FrmGroupNapXuAndExchangeGold.backForm != null) {
        Global.nextState(FrmGroupNapXuAndExchangeGold.backForm, null);
        FrmGroupNapXuAndExchangeGold.backForm = null;
      } else {
        if (Global.systemCanvas.lastState instanceof FrmDownloadResource) {
          GameGlobal.nextState(FrmGroupMenu.instance(), null);
        } else {
          GameGlobal.nextState(Global.systemCanvas.lastState, null);
        }
      }
      break;
    case Key.SOFT_LEFT:
      if (moneyTextField.getText().length() == 0) {
        Global.alert.showAlert(this, GameAlert.OK_TYPE, "Hãy nhập số tiền cần đổi");
        return;
      }
      
      int money = NumberUtil.parseInt(moneyTextField.getText());
      if (money == NumberUtil.ERROR_VALUE) {
        Global.alert.showAlert(this, GameAlert.OK_TYPE, "Tiền phải là số");
        return;
      }
        
      
      if (money == 0) {
        Global.alert.showAlert(this, GameAlert.OK_TYPE, "Hãy nhập số tiền cần đổi");
        return;
      }
      
      if (money > Global.currentUser.getMoney()) {
        Global.alert.showAlert(this, GameAlert.OK_TYPE, "Bạn không có đủ tiền");
        return;
      }
      
      // Xác nhận người dùng có muốn đổi tiền sang vàng không 
      Global.alert.showAlert1(this, GameAlert.YES_NO_TYPE, StringArray.create1("Bạn có muốn đổi " + money + " tiền", " thành " + gold + " gold?"), 22);
      break;
    default:
      moneyTextField.keyReleased(null, keyCode);
      break;
    }
  }
  
  public void onActionPerform(Event event) {
    if (MultiTab.OPEN_TAB_EVENT.equals(event.getAction())) {
      DrawUtils.setBottomBar(_manager, "Quy đổi", "Sửa", "Trở về");
      return;
    }
  }
  
  public void alertEventPerform(int alertType, int eventType, int alertId) {
    // Nếu người dùng đồng ý đổi tiền sang vàng
    if ((eventType == GameAlert.YES_BUTTON) && (alertId == 22)) {
      int money = NumberUtil.parseInt(moneyTextField.getText());
      if (money == NumberUtil.ERROR_VALUE) {
        Global.alert.showAlert(this, GameAlert.OK_TYPE, "Tiền phải là số");
        return;
      }
      SocketClientUtil.changeMoneyToGoldRequest(money);
      Global.alert.showAlert(this, GameAlert.LOADING_WITH_NO_BUTTON_TYPE, "Đang đổi tiền thành gold...");
    }
  }
  
  public void doTask() {
    if (!isReceivedExchangeRate) {
      return;
    }
    
    int money = NumberUtil.parseInt(moneyTextField.getText());
    if (money == NumberUtil.ERROR_VALUE) {
      money = 0;
    }
    gold = (int) (exchangeRate * money);
  }
  
  public void destroy() {
    if (moneyTextField != null) {
      moneyTextField.detroy();
      moneyTextField = null;
    }
  }
}
